package com.tools.xxf.updatelib.apkinfo.fragment;

import android.text.TextUtils;

import com.tools.xxf.updatelib.apkinfo.ApkData;
import com.tools.xxf.updatelib.apkinfo.ApkInFoBean;
import com.tools.xxf.updatelib.apkinfo.ApkToken;

import rx.Subscriber;

/**
 * TODO 获取服务器版本信息的接口
 *
 * @author XXF
 *         Create Time : 2017/11/7 10:39
 */
public class UpdatePresenter {

    private final ApkData instance;

    /**
     * 完美的单例模式
     * 线程安全的懒汉模式
     */
    public static UpdatePresenter getInstance() {
        return Holder.INSTANCE;
    }

    private static class Holder {
        private static final UpdatePresenter INSTANCE = new UpdatePresenter();
    }

    public UpdatePresenter() {
        instance = ApkData.getInstance();
    }

    public void getToken(final String version, final UpdateCall call, final String packageName) {
        instance.getToken(new Subscriber<ApkToken>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                call.getTokenFail(e);
            }

            @Override
            public void onNext(ApkToken apkToken) {
                if (apkToken != null && !TextUtils.isEmpty(apkToken.getToken())) {
                    getAPKinfo(apkToken.getToken(), version, call, packageName);
                } else {
                    String msg = "";
                    if (apkToken != null && apkToken.getMessage() != null)
                        msg = apkToken.getMessage();
                    else
                        msg = "Server unknown exception! ";
                    Throwable throwable = new Throwable(msg);
                    call.getTokenFail(throwable);
                }
            }
        }, "7940010a2cf2eeb6f106d9b12ddb510c", "39bcc243eb9ef9a3a00c3eb1a072bf9c");
    }

    public void getAPKinfo(String token, String version, final UpdateCall call, String packageName) {
        instance.getAPKinfo(new Subscriber<ApkInFoBean>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
                call.getAPKinfoFail(e);
            }

            @Override
            public void onNext(ApkInFoBean apkInFoBean) {
                if (apkInFoBean != null && apkInFoBean.getData() != null) {
                    call.getAPKinfoSuccessful(apkInFoBean);
                } else {
                    String msg = "";
                    if (apkInFoBean != null && apkInFoBean.getMessage() != null)
                        msg = apkInFoBean.getMessage();
                    else
                        msg = "Server unknown exception! ";
                    Throwable throwable = new Throwable(msg);
                    call.getAPKinfoFail(throwable);
                }

            }
        }, packageName, version, token);
    }

    public interface UpdateCall {

        void getTokenFail(Throwable e);

        void getAPKinfoFail(Throwable e);

        void getAPKinfoSuccessful(ApkInFoBean bean);
    }
}
